package chapt03.scissor;
// PStipple.java
// OpenGL SuperBible
// Demonstrates OpenGL Polygon Stippling
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class Scissor
{
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Set the perspective coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Set 2D Coordinate system
		GLU glu = new GLU();
		glu.gluOrtho2D(-4.0, 4.0, -3.0, 3.0);
		
		// Modelview matrix reset
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Clear blue window
		gl2.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Now set scissor to smaller red sub region
		gl2.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
		gl2.glScissor(60, 60, w - 120, h - 120);
		gl2.glEnable(GL2.GL_SCISSOR_TEST);
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Finally, an even smaller green rectangle
		gl2.glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
		gl2.glScissor(120, 120, w - 240, h - 240);
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Turn scissor back off for next render
		gl2.glDisable(GL2.GL_SCISSOR_TEST);
		
		// Restore transformations
		gl2.glPopMatrix();
		
		// Flush drawing commands
		gl2.glFlush();
		
	}
}
